﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Utilities;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;
using System.Collections;

namespace FinanSoft.PresentacionInventario
{
    public partial class frmCrearBodega : Form
    {
        int _editedStore;        
        List<int> _accountIds;
        bool _codeChange;

        public frmCrearBodega(int currentId)
        {
            InitializeComponent();
            _editedStore = currentId;
            fillFields();
            _codeChange = false;
        }

        private void fillFields()
        {
            int accIndex;

            if (_editedStore != Constants.INVALID_ID)
            {
                fillAccounts();
                StoreRoom editedStore = StoreRoomManager.GetInstance().GetStoreRoom(_editedStore);

                accIndex = _accountIds.IndexOf(editedStore.InventoryAccount.AccountID);
                cbInvAcc.SelectedIndex = accIndex;

                accIndex = _accountIds.IndexOf(editedStore.PurchasesAccount.AccountID);
                cbPurchasesAcc.SelectedIndex = accIndex;

                accIndex = _accountIds.IndexOf(editedStore.SalesAccount.AccountID);
                cbSalesAcc.SelectedIndex = accIndex;

                accIndex = _accountIds.IndexOf(editedStore.SalesCostAccount.AccountID);
                cbSalesCostAcc.SelectedIndex = accIndex;

                txtName.Text = editedStore.Name;
                txtCode.Text = editedStore.Code;
            }
            else
            {
                fillAccounts();
            }
        }

        private void fillAccounts()
        {
            AccountingCatalog catalog;

            cbInvAcc.Items.Clear();
            cbPurchasesAcc.Items.Clear();
            cbSalesAcc.Items.Clear();
            cbSalesCostAcc.Items.Clear();

            _accountIds = new List<int>();
            catalog = CatalogManager.GetInstance().GetAccountingCatalog();
            foreach (Account account in catalog.PrincipalAccounts)
                addAccount(account);
            
            cbInvAcc.SelectedIndex = 0;
            cbPurchasesAcc.SelectedIndex = 0;
            cbSalesAcc.SelectedIndex = 0;
            cbSalesCostAcc.SelectedIndex = 0;
        }

        private void addAccount(Account account)
        {
            if (!account.IsTitle)
            {
                _accountIds.Add(account.AccountID);
                cbInvAcc.Items.Add(account.DisplayName);
                cbPurchasesAcc.Items.Add(account.DisplayName);
                cbSalesAcc.Items.Add(account.DisplayName);
                cbSalesCostAcc.Items.Add(account.DisplayName);
            }
            else
                foreach (Account child in account.Children.Values)
                    addAccount(child);
        }

        public void CreateStoreRoom()
        {
            if (ValidateFields() && ValidateInsertConditions())
            {
                StoreRoomManager.GetInstance().CreateAndRegisterStorage(txtCode.Text, txtName.Text, _accountIds[cbSalesAcc.SelectedIndex],
                    _accountIds[cbPurchasesAcc.SelectedIndex], _accountIds[cbInvAcc.SelectedIndex], _accountIds[cbSalesCostAcc.SelectedIndex]);

                if (StoreRoomManager.GetInstance().IsError)
                    MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(MessageConstants.STORE_INSERTED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
 
        }

        public void UpdateStoreRoom()
        {
            if (ValidateFields() && ValidateUpdateConditions())
            {
                StoreRoomManager.GetInstance().UpdateStorage(_editedStore, txtCode.Text, txtName.Text, _accountIds[cbSalesAcc.SelectedIndex],
                    _accountIds[cbPurchasesAcc.SelectedIndex], _accountIds[cbInvAcc.SelectedIndex], _accountIds[cbSalesCostAcc.SelectedIndex]);

                if (StoreRoomManager.GetInstance().IsError)
                    MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(MessageConstants.STORE_UPDATED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        public bool ValidateUpdateConditions()
        {            
            if(_codeChange && StoreRoomManager.GetInstance().CheckCode(txtCode.Text))
            {
                MessageBox.Show(MessageConstants.CODE_UPDATED_EXISTS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false; 
            }

            return true;
        }

        public bool ValidateFields()
        {
            if (txtName.Text.Trim().Length == 0 || txtCode.Text.Trim().Length == 0 || cbInvAcc.SelectedIndex == Constants.INVALID_ID || cbSalesAcc.SelectedIndex == Constants.INVALID_ID
                || cbPurchasesAcc.SelectedIndex == Constants.INVALID_ID || cbSalesCostAcc.SelectedIndex == Constants.INVALID_ID)
            {
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        public bool ValidateInsertConditions()
        {            
            if(StoreRoomManager.GetInstance().CheckCode(txtCode.Text))
            {
                MessageBox.Show(MessageConstants.NEW_CODE_EXIST, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false; 
            }

            return true;
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCrear_Click(object sender, EventArgs e)
        {
            if (_editedStore == Constants.INVALID_ID)
            {
                CreateStoreRoom();
                this.Close();
            }
            else
            {
                UpdateStoreRoom();
                this.Close();
            }
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {
            _codeChange = true;
        }
    }
}
